<template>
  <div>
    <HeadTag :tagName="tagName" :curTag="curTag"></HeadTag>
    <el-divider></el-divider>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>公告</span>
      </div>
      <div v-for="val in showData" :key="val.n_id" class="text item main">
        <p><span>{{val.n_title}}</span> <span>{{val.n_pubdate}}</span></p>
        <p>{{val.n_content}}</p>
      </div>
      <el-pagination layout="prev, pager, next" :total="PageInfo.total" :page-size="PageInfo.pageSize" :current-page.sync="PageInfo.currentPage" @current-change="handleCurrentChange" align="center">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      tagName: [],
      curTag: '公告主页',
      showContent: [],
      showData: [],
      PageInfo: {
        total: 10,
        pageSize: 3,
        currentPage: 1
      },
    };
  },
  methods: {
    async getNotice () {
      const result = await this.$http.get('/getAnnouncement')
      result.data.forEach((val) => {
        val.n_pubdate = this.$formatDate(val.n_pubdate)
      })
      this.showContent = result.data
      this.PageInfo.total = this.showContent.length
      this.showTable()
    },
    handleCurrentChange (val) {
      this.PageInfo.currentPage = val
      this.showTable()
    },
    showTable () {
      let start = this.PageInfo.pageSize * (this.PageInfo.currentPage - 1)
      this.showData = this.showContent.filter((val, index) => {
        if (index >= start && index < this.PageInfo.pageSize + start) {
          return val
        }
      })
    },
  },
  created () {
    this.$store.commit('addTag', { name: '公告主页', router: 'welcome' })
    this.tagName = this.$store.state.tagNames
    this.getNotice()
  },
};
</script>

<style scoped>
.main{
  border-bottom: 1px solid rgb(232,232,232);
}
.main p:first-of-type{
  height: 30px;
  line-height: 30px;
}
.main p:last-of-type{
  font-size: 14px;
  color: rgb(158,155,166);
}
.main span:first-of-type{
  font-size: 16px;
  font-weight: bold;
}
.main span:last-of-type{
  float: right;
  font-size: 12px;
  color: rgb(158,155,166);
}
</style>
